package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.DateUtils;
import com.hyphenate.util.TextFormater;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.im.model.NormalFileMessageBody;
import com.molyfun.parents.service.MediaUtils;
import com.molyfun.parents.utils.StringUtils;
import com.squareup.picasso.Picasso;

import java.io.File;

import static com.molyfun.parents.utils.Const.VIDEO_COVER_SUFFIX;

public class ChatRowVideo extends ChatRowFile {

    private ImageView imageView;
    private TextView sizeView;
    private TextView timeLengthView;
    private TextView mNickName;
    private boolean mShowNickName;

    public ChatRowVideo(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public ChatRowVideo(Context context, ChatMessageEntity message, int position, boolean showNickName, BaseAdapter adapter) {
        super(context, message, position, adapter);
        mShowNickName = showNickName;
    }


    @Override
    protected void onInflateView() {
        if (message.isUseNative()) {
            inflater.inflate(R.layout.ease_row_sent_video, this);
        } else {
            inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                    R.layout.ease_row_received_video : R.layout.ease_row_sent_video, this);
        }
    }

    @Override
    protected void onFindViewById() {
        imageView = ((ImageView) findViewById(R.id.chatting_content_iv));
        sizeView = (TextView) findViewById(R.id.chatting_size_iv);
        timeLengthView = (TextView) findViewById(R.id.chatting_length_iv);
        ImageView playView = (ImageView) findViewById(R.id.chatting_status_btn);
        percentageView = (TextView) findViewById(R.id.percentage);
        mNickName = (TextView) findViewById(R.id.tv_userid);
    }

    @Override
    protected void onSetUpView() {
        NormalFileMessageBody videoBody = (NormalFileMessageBody) message.getBody();
        ChatMessageEntity.ErrorNativeBody errorNativeBody = message.getErrorNativeBody();

        if(message.isUseNative()){
            imageView.setImageResource(R.drawable.ease_default_image);
        }else{
//            String localThumb = videoBody.getLocalThumb();
            if (!TextUtils.isEmpty(videoBody.getThumbnailUrl())) {
                Picasso.with(context)
                        .load(videoBody.getThumbnailUrl())
                        .placeholder(R.drawable.ease_default_image)
                        .into(imageView);
            } else {
                if (!TextUtils.isEmpty(videoBody.getRemoteUrl())) {
                    Picasso.with(context)
                            .load(videoBody.getRemoteUrl() + VIDEO_COVER_SUFFIX)
                            .placeholder(R.drawable.ease_default_image)
                            .into(imageView);
                } else {
                    imageView.setImageResource(R.drawable.ease_default_image);
                }
            }

//            showVideoThumbView(localThumb, imageView, videoBody.getThumbnailUrl(), message);
//        }

            if (videoBody.getLength() > 0) {
                String time = DateUtils.toTime(videoBody.getLength());
                timeLengthView.setText(time);
            }

            if (message.direct() == EMMessage.Direct.RECEIVE) {
                if (videoBody.getFileSize() > 0) {
                    String size = TextFormater.getDataSize(videoBody.getFileSize());
                    sizeView.setText(size);
                }
            } else {
                if (videoBody.getLocalUrl() != null && new File(videoBody.getLocalUrl()).exists()) {
                    String size = TextFormater.getDataSize(new File(videoBody.getLocalUrl()).length());
                    sizeView.setText(size);
                }
            }
        }

        if(mShowNickName) {
            if(message.direct() == EMMessage.Direct.RECEIVE){
                mNickName.setText(StringUtils.getSpltString(message.getUsername(), 10));
            }else{
                mNickName.setText("[" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getStudentname(), 10) + "]" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getUsername(), 10));
            }
        }

        //handle sending message
//        handleSendMessage();
    }

    @Override
    protected void onBubbleClick() {
        NormalFileMessageBody videoBody = (NormalFileMessageBody) message.getBody();
//        EMLog.d(TAG, "video view is on click");
//        Intent intent = new Intent(context, EaseShowVideoActivity.class);
//        intent.putExtra("msg", message);
//        if (message != null && message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked()
//                && message.getChatType() == ChatType.Chat) {
//            try {
//                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        activity.startActivity(intent);
        if(message.isUseNative()){
            ChatMessageEntity.ErrorNativeBody errorNativeBody = message.getErrorNativeBody();
            if(errorNativeBody != null){
                MediaUtils.startVideoAndStopAudio(context, errorNativeBody.getFilePath(), videoBody.getFileName());
            }
        }else{
            MediaUtils.startVideoAndStopAudio(context, videoBody.getRemoteUrl(), videoBody.getFileName());
        }
    }

    /**
     * show video thumbnails
     *
     * @param localThumb
     *            local path for thumbnail
     * @param iv
     * @param thumbnailUrl
     *            Url on server for thumbnails
     * @param message
     */

}
